package net.hzlj.platform.enterprise.utils.recorder;

import android.media.AudioFormat;
import android.media.MediaRecorder;

import java.io.File;
import java.util.UUID;

import omrecorder.AudioChunk;
import omrecorder.AudioSource;
import omrecorder.OmRecorder;
import omrecorder.PullTransport;
import omrecorder.Recorder;

/**
 * Created by Administrator on 2016/8/22 0022.
 */
public class WavOmRecorderAudioImpl implements RecorderAudioService {

//    private ExtAudioRecorder mRecorder;
    private Recorder mRecorder;
    private File mDir;// 保存录音的文件夹
    private String mCurrentFilePath;// 录音文件path
    private boolean isPrepared;// 是否已经准备完毕
    private RecorderAudioManager.AudioStateListener mListener;

    private int mMaxAmplitude;


    @Override
    public void setOnAudioStateListener(RecorderAudioManager.AudioStateListener listener) {
        this.mListener = listener;
    }

    @Override
    public void setRecorderFileDir(File dir) {
        this.mDir = dir;
    }


    @Override
    public void prepareAudio() {
        try {
            isPrepared = false;
            String fileName = generateFileName();
            File file = new File(mDir, fileName);
            mCurrentFilePath = file.getAbsolutePath();
            mRecorder = OmRecorder.wav(
                    new PullTransport.Default(mic(), new PullTransport.OnAudioChunkPulledListener() {
                        @Override
                        public void onAudioChunkPulled(AudioChunk audioChunk) {
                            mMaxAmplitude = (int) (audioChunk.maxAmplitude() / 200.0d);
//                            animateVoice((float) (audioChunk.maxAmplitude() / 200.0));
                        }
                    }), new File(mCurrentFilePath));
            mRecorder.startRecording();
            isPrepared = true;// 准备结束
            if (null != mListener) {
                mListener.wellPrepared();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void release() {
        try{
            if (mRecorder != null){
                mRecorder.stopRecording();
                mRecorder = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            mRecorder = null;
        }
    }

    @Override
    public void cancel() {
        if (null != mCurrentFilePath) {
            File file = new File(mCurrentFilePath);
            file.delete();
            mCurrentFilePath = null;
        }
        release();
    }

    @Override
    public String getCurrentFilePath() {
        return mCurrentFilePath;
    }

    @Override
    public int getVoiceLevel(int maxLevel) {
        /*int level = 1;
        if (isPrepared && null != mRecorder) {
            // 获得它最大的震幅 getMaxAmplitude()范围是1~32767
            level = mRecorder.getMaxAmplitude();
        }*/
        return mMaxAmplitude;
    }

    /**
     * 随机生成文件名称
     *
     * @return
     */
    private String generateFileName() {
        return UUID.randomUUID().toString() + ".wav";
    }

    private AudioSource mic() {
        return new AudioSource.Smart(MediaRecorder.AudioSource.MIC, AudioFormat.ENCODING_PCM_16BIT,
                AudioFormat.CHANNEL_IN_MONO, 44100);
    }

}
